package com.isoft.airportsystem.service.impl.Flight;

import com.isoft.airportsystem.entity.flight.BaggageInfo;
import com.isoft.airportsystem.entity.flight.FlightInfo;
import com.isoft.airportsystem.mapper.Flight.FlightMapper;
import com.isoft.airportsystem.service.flight.FlightService;
import com.isoft.airportsystem.util.BeanUtil;
import com.isoft.airportsystem.util.DataConvertUtil;
import com.isoft.airportsystem.util.ExcelUtil;
import com.isoft.airportsystem.util.PageResult;
import com.isoft.airportsystem.vo.FlightQueryVO;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class FlightServiceImpl implements FlightService {

    @Autowired
    private FlightMapper flightMapper;

    @Override
    public int getTotalFlightCount() {
        return 0;
    }

    @Override
    public int getTodayFlightCount() {
        return 0;
    }

    @Override
    public Map<String, Integer> getFlightStatusDistribution() {
        return Map.of();
    }

    @Override
    public Map<String, Integer> getFlightTypeDistribution() {
        return Map.of();
    }

    @Override
    public List<Integer> getWeeklyFlightCounts(List<String> dates) {
        return List.of();
    }

    @Override
    public List<FlightInfo> queryFlightList(FlightQueryVO queryVO) {
        return flightMapper.selectFlightList(queryVO);
    }

    @Override
    public PageResult<FlightInfo> queryFlightPage(FlightQueryVO queryVO, int pageNum, int pageSize) {
        Map<String, Object> params = BeanUtil.beanToMap(queryVO);
        params.put("offset", (pageNum - 1) * pageSize);
        params.put("limit", pageSize);

        List<FlightInfo> list = flightMapper.selectFlightPage(params);
        int total = flightMapper.countFlights(params);

        return new PageResult<>(total, list);
    }

    @Override
    public FlightInfo getFlightDetail(String flightId) {
        return flightMapper.selectFlightById(flightId);
    }

    @Override
    public List<BaggageInfo> getBaggageInfo(String flightId) {
        return flightMapper.selectBaggageByFlightId(flightId);
    }

    @Override
    public Map<String, Object> getFlightStatusStats() {
        // 使用原始List接收
        List<Map<String, Object>> rawStats = flightMapper.selectFlightStatusStats();

        // 手动转换为需要的格式
        Map<String, Object> result = new LinkedHashMap<>();
        for (Map<String, Object> stat : rawStats) {
            String status = String.valueOf(stat.get("status")); // 确保转为String
            Integer count = ((Number)stat.get("count")).intValue(); // 处理数字类型
            result.put(status, count);
        }
        return result;
    }


    @Override
    public Map<String, Object> getDelayStats() {
        List<Map<String, Object>> stats = flightMapper.selectDelayStats();
//         转换为前端需要的格式
        return DataConvertUtil.convertStatsToMap(stats);

    }

    @Override
    public List<FlightInfo> getRecentFlights(int limit) {
        return flightMapper.selectRecentFlights(limit);
    }

    @Override
    public void exportFlightData(FlightQueryVO queryVO, HttpServletResponse response) {
        List<FlightInfo> list = queryFlightList(queryVO);
        ExcelUtil.export(list, "航班数据", "航班信息", FlightInfo.class, response);
    }

    @Override
    public void deleteFlight(String flightId) {
        System.out.println("开始删除航班，flightId: " + flightId);
        try {
            int result = flightMapper.deleteFlight(flightId);
            System.out.println("删除航班结果，影响行数: " + result);
        } catch (Exception e) {
            System.err.println("删除航班异常: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void addFlight(FlightInfo flightInfo) {
        if (flightInfo.getFlightId() == null || flightInfo.getFlightId().isEmpty()) {
            flightInfo.setFlightId(java.util.UUID.randomUUID().toString().replace("-", ""));
        }
        flightMapper.insertFlight(flightInfo);
    }

    @Override
    public void updateFlight(FlightInfo flightInfo) {
        flightMapper.updateFlight(flightInfo);
    }
}